﻿using System.Text.Json.Serialization;
using BCore.Identity.Users.Caching;
using Core.Identity.Common.Interfaces.Common.Interfaces;
using Core.Identity.Menus.Caching;
using Core.Identity.Users.Dtos;
using Core.Models;
using Mapster;
using MediatR;

namespace Core.Identity.Users.Features.Queries.GetMine;

/// <summary>
/// 我的信息
/// </summary>
public class GetMineQuery : IRequest<Result<UserDto>>
{
    /// <summary>
    /// 登录者
    /// </summary>
    public Guid UserId { get; set; }

    /// <summary>
    /// 缓存键
    /// </summary>
    [JsonIgnore]
    public string CacheKey => UsersCacheKey.GetMineCacheKey(UserId.ToString());
    /// <summary>
    /// 缓存选项
    /// </summary>
    [JsonIgnore]
    public IEnumerable<string>? Tags => MenusCacheKey.Tags;
}

public class GetMineQueryHandler(
    IIdentityDbContext context) : IRequestHandler<GetMineQuery, Result<UserDto>>
{

    public async Task<Result<UserDto>> Handle(GetMineQuery request, CancellationToken cancellationToken)
    {
        var user = context.Users
            .FirstOrDefault(x => x.Id == request.UserId);

        return await Result<UserDto>.SucceedAsync(user.Adapt<UserDto>());
    }
}
